package database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Book {
	private Connection connection = null;

	public Book(Connection connection) {
		this.connection=connection;
		if(this.connection.equals(null)){
			System.out.println("Connection is not ready when creating class Flashcard");
		}
	}
	public ArrayList<String> getBookNames(){
		String query = "SELECT name FROM chen869.book";	
			ArrayList<String> names = new ArrayList<String>();
			try{
				Statement st = connection.createStatement();
				ResultSet rs = st.executeQuery(query);
				while(rs.next()){
					names.add(rs.getString("name"));
				}
			}
			catch(SQLException e){
				System.out.println("selectFront has gone wrong");
				return null;
			}
			return names;
	}
	public String getBookNum(){
		String query = "SELECT COUNT( * ) FROM chen869.book";
		String num = "empty";
		try {
			Statement st = connection.createStatement();
			ResultSet rs = st.executeQuery(query);
			while (rs.next()) {
				num = rs.getString(1);
				//System.out.println("num:"+num);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("select has gone wrong");
			return null;
		}
		return num;
	}
	public void insertBook(String bookName){
	
		
		String query = "INSERT INTO chen869.book (name) VALUES (\'"+bookName+"\')";
		try{
			Statement st = connection.createStatement();
			st.executeUpdate(query);
		}catch(SQLException e){
			System.out.println("InsertBook has gone wrong");
		}
			
		System.out.println(query);
		return;
	}
}
